'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
	
	const res_note = await db.collection('rx-visit-note').where({ userId: event.userId }).get()
	
	if ( event.request === 'GET' ) {
		if ( res_note.affectedDocs === 0 ) {
			return { data: '', code: 200, msg: 'ok.' }	//返回数据给客户端
		}
		return { data: res_note.data[0].context, code: 200, msg: 'ok.' }	//返回数据给客户端
	}
	
	if ( event.request === 'POST' ) {
		if ( res_note.affectedDocs === 0 ) {
			await db.collection('rx-visit-note').add({ userId: event.userId, context: event.context })
		} else {
			await db.collection('rx-visit-note').where({ userId: event.userId }).update({ context: event.context })
		}
		return { data: {}, code: 200, msg: 'ok.' }	//返回数据给客户端
	}
	
};
